package cn.zpcnet.zstring.utils.read;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author zpc
 * @version 1.0
 */
public class ReadFileUtil implements ReadFile {
    private static final ReadFileUtil instance = new ReadFileUtil();


    /**
     * 读取文件 并去除空格
     * @param fileInput 文件输入流
     * @return  String
     * @throws IOException 抛出IO异常
     */
    public static String read(FileInputStream fileInput) throws IOException {
        return instance.readTrim(fileInput);
    }

    /**
     * 读取文件 并去除空格
     * @param fileInput 文件输入流
     * @return  String
     * @throws IOException 抛出IO异常
     */
    public static String[] readDelimiter(FileInputStream fileInput,String delimiter) throws IOException {
        return instance.readByDelimiter(fileInput,delimiter);
    }

    /**
     * 读取文件 并去除空格
     * @param fileInput 文件输入流
     * @return  String
     * @throws IOException 抛出IO异常
     */
    public static String[] readDelimiter(FileInputStream fileInput) throws IOException {
        return instance.readByDelimiter(fileInput);
    }
    
    
    @Override
    public String readTrim(FileInputStream fileInput) throws IOException {
        StringBuilder r = new StringBuilder();
        while (fileInput.available() > 0) {
            r.append((char) fileInput.read());
        }
        return r.toString().trim();
    }

    @Override
    public String[] readByDelimiter(FileInputStream fileInput, String delimiter) throws IOException {
        String r = readTrim(fileInput);
        return r.split(delimiter);
    }

    @Override
    public String[] readByDelimiter(FileInputStream fileInput) throws IOException {
        return readByDelimiter(fileInput, ",");
    }
}
